home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / elf32-mips.c < prev    next >
C/C++ Source or Header  |  1994-09-07  |  73KB  |  2,471 lines

  1. /* MIPS-specific support for 32-bit ELF
  2.    Copyright 1993, 1994 Free Software Foundation, Inc.
  3.  
  4.    Most of the information added by Ian Lance Taylor, Cygnus Support,
  5.    <ian@cygnus.com>.
  6.  
  7. This file is part of BFD, the Binary File Descriptor library.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. #include "bfd.h"
  24. #include "sysdep.h"
  25. #include "libbfd.h"
  26. #include "bfdlink.h"
  27. #include "genlink.h"
  28. #include "libelf.h"
  29. #include "elf/mips.h"
  30.  
  31. /* Get the ECOFF swapping routines.  */
  32. #include "coff/sym.h"
  33. #include "coff/symconst.h"
  34. #include "coff/internal.h"
  35. #include "coff/ecoff.h"
  36. #include "coff/mips.h"
  37. #define ECOFF_32
  38. #include "ecoffswap.h"
  39.  
  40. static bfd_reloc_status_type mips_elf_hi16_reloc PARAMS ((bfd *abfd,
  41.                               arelent *reloc,
  42.                               asymbol *symbol,
  43.                               PTR data,
  44.                               asection *section,
  45.                               bfd *output_bfd,
  46.                               char **error));
  47. static bfd_reloc_status_type mips_elf_got16_reloc PARAMS ((bfd *abfd,
  48.                                arelent *reloc,
  49.                                asymbol *symbol,
  50.                                PTR data,
  51.                                asection *section,
  52.                                bfd *output_bfd,
  53.                                char **error));
  54. static bfd_reloc_status_type mips_elf_lo16_reloc PARAMS ((bfd *abfd,
  55.                               arelent *reloc,
  56.                               asymbol *symbol,
  57.                               PTR data,
  58.                               asection *section,
  59.                               bfd *output_bfd,
  60.                               char **error));
  61. static bfd_reloc_status_type mips_elf_gprel16_reloc PARAMS ((bfd *abfd,
  62.                                  arelent *reloc,
  63.                                  asymbol *symbol,
  64.                                  PTR data,
  65.                                  asection *section,
  66.                                  bfd *output_bfd,
  67.                                  char **error));
  68. static const struct reloc_howto_struct *bfd_elf32_bfd_reloc_type_lookup
  69.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  70. static void mips_info_to_howto_rel
  71.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
  72. static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
  73. static boolean mips_elf_object_p PARAMS ((bfd *));
  74. static void mips_elf_final_write_processing
  75.   PARAMS ((bfd *, boolean));
  76. static boolean mips_elf_section_from_shdr
  77.   PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
  78. static boolean mips_elf_fake_sections
  79.   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
  80. static boolean mips_elf_section_from_bfd_section
  81.   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
  82. static boolean mips_elf_section_processing
  83.   PARAMS ((bfd *, Elf32_Internal_Shdr *));
  84. static void mips_elf_symbol_processing PARAMS ((bfd *, asymbol *));
  85. static boolean mips_elf_read_ecoff_info
  86.   PARAMS ((bfd *, asection *, struct ecoff_debug_info *));
  87. static struct bfd_hash_entry *mips_elf_link_hash_newfunc
  88.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  89. static struct bfd_link_hash_table *mips_elf_link_hash_table_create
  90.   PARAMS ((bfd *));
  91. static int gptab_compare PARAMS ((const void *, const void *));
  92. static boolean mips_elf_final_link
  93.   PARAMS ((bfd *, struct bfd_link_info *));
  94. static void mips_elf_relocate_hi16
  95.   PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
  96.        bfd_vma));
  97. static boolean mips_elf_relocate_section
  98.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  99.        Elf_Internal_Rela *, Elf_Internal_Sym *, asection **, char *));
  100. static boolean mips_elf_add_symbol_hook
  101.   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
  102.        const char **, flagword *, asection **, bfd_vma *));
  103.  
  104. #define USE_REL    1        /* MIPS uses REL relocations instead of RELA */
  105.  
  106. enum reloc_type
  107. {
  108.   R_MIPS_NONE = 0,
  109.   R_MIPS_16,        R_MIPS_32,
  110.   R_MIPS_REL32,        R_MIPS_26,
  111.   R_MIPS_HI16,        R_MIPS_LO16,
  112.   R_MIPS_GPREL16,    R_MIPS_LITERAL,
  113.   R_MIPS_GOT16,        R_MIPS_PC16,
  114.   R_MIPS_CALL16,    R_MIPS_GPREL32,
  115.   R_MIPS_max
  116. };
  117.  
  118. static reloc_howto_type elf_mips_howto_table[] =
  119. {
  120.   /* No relocation.  */
  121.   HOWTO (R_MIPS_NONE,        /* type */
  122.      0,            /* rightshift */
  123.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  124.      0,            /* bitsize */
  125.      false,            /* pc_relative */
  126.      0,            /* bitpos */
  127.      complain_overflow_dont, /* complain_on_overflow */
  128.      bfd_elf_generic_reloc,    /* special_function */
  129.      "R_MIPS_NONE",        /* name */
  130.      false,            /* partial_inplace */
  131.      0,            /* src_mask */
  132.      0,            /* dst_mask */
  133.      false),        /* pcrel_offset */
  134.  
  135.   /* 16 bit relocation.  */
  136.   HOWTO (R_MIPS_16,        /* type */
  137.      0,            /* rightshift */
  138.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  139.      16,            /* bitsize */
  140.      false,            /* pc_relative */
  141.      0,            /* bitpos */
  142.      complain_overflow_bitfield, /* complain_on_overflow */
  143.      bfd_elf_generic_reloc,    /* special_function */
  144.      "R_MIPS_16",        /* name */
  145.      true,            /* partial_inplace */
  146.      0xffff,        /* src_mask */
  147.      0xffff,        /* dst_mask */
  148.      false),        /* pcrel_offset */
  149.  
  150.   /* 32 bit relocation.  */
  151.   HOWTO (R_MIPS_32,        /* type */
  152.      0,            /* rightshift */
  153.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  154.      32,            /* bitsize */
  155.      false,            /* pc_relative */
  156.      0,            /* bitpos */
  157.      complain_overflow_bitfield, /* complain_on_overflow */
  158.      bfd_elf_generic_reloc,    /* special_function */
  159.      "R_MIPS_32",        /* name */
  160.      true,            /* partial_inplace */
  161.      0xffffffff,        /* src_mask */
  162.      0xffffffff,        /* dst_mask */
  163.      false),        /* pcrel_offset */
  164.  
  165.   /* 32 bit symbol relative relocation.  */
  166.   HOWTO (R_MIPS_REL32,        /* type */
  167.      0,            /* rightshift */
  168.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  169.      32,            /* bitsize */
  170.      false,            /* pc_relative */
  171.      0,            /* bitpos */
  172.      complain_overflow_bitfield, /* complain_on_overflow */
  173.      bfd_elf_generic_reloc,    /* special_function */
  174.      "R_MIPS_REL32",    /* name */
  175.      true,            /* partial_inplace */
  176.      0xffffffff,        /* src_mask */
  177.      0xffffffff,        /* dst_mask */
  178.      false),        /* pcrel_offset */
  179.  
  180.   /* 26 bit branch address.  */
  181.   HOWTO (R_MIPS_26,        /* type */
  182.      2,            /* rightshift */
  183.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  184.      26,            /* bitsize */
  185.      false,            /* pc_relative */
  186.      0,            /* bitpos */
  187.      complain_overflow_dont, /* complain_on_overflow */
  188.                  /* This needs complex overflow
  189.                    detection, because the upper four
  190.                    bits must match the PC.  */
  191.      bfd_elf_generic_reloc,    /* special_function */
  192.      "R_MIPS_26",        /* name */
  193.      true,            /* partial_inplace */
  194.      0x3ffffff,        /* src_mask */
  195.      0x3ffffff,        /* dst_mask */
  196.      false),        /* pcrel_offset */
  197.  
  198.   /* High 16 bits of symbol value.  */
  199.   HOWTO (R_MIPS_HI16,        /* type */
  200.      0,            /* rightshift */
  201.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  202.      16,            /* bitsize */
  203.      false,            /* pc_relative */
  204.      0,            /* bitpos */
  205.      complain_overflow_dont, /* complain_on_overflow */
  206.      mips_elf_hi16_reloc,    /* special_function */
  207.      "R_MIPS_HI16",        /* name */
  208.      true,            /* partial_inplace */
  209.      0xffff,        /* src_mask */
  210.      0xffff,        /* dst_mask */
  211.      false),        /* pcrel_offset */
  212.  
  213.   /* Low 16 bits of symbol value.  */
  214.   HOWTO (R_MIPS_LO16,        /* type */
  215.      0,            /* rightshift */
  216.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  217.      16,            /* bitsize */
  218.      false,            /* pc_relative */
  219.      0,            /* bitpos */
  220.      complain_overflow_dont, /* complain_on_overflow */
  221.      mips_elf_lo16_reloc,    /* special_function */
  222.      "R_MIPS_LO16",        /* name */
  223.      true,            /* partial_inplace */
  224.      0xffff,        /* src_mask */
  225.      0xffff,        /* dst_mask */
  226.      false),        /* pcrel_offset */
  227.  
  228.   /* GP relative reference.  */
  229.   HOWTO (R_MIPS_GPREL16,    /* type */
  230.      0,            /* rightshift */
  231.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  232.      16,            /* bitsize */
  233.      false,            /* pc_relative */
  234.      0,            /* bitpos */
  235.      complain_overflow_signed, /* complain_on_overflow */
  236.      mips_elf_gprel16_reloc, /* special_function */
  237.      "R_MIPS_GPREL16",    /* name */
  238.      true,            /* partial_inplace */
  239.      0xffff,        /* src_mask */
  240.      0xffff,        /* dst_mask */
  241.      false),        /* pcrel_offset */
  242.  
  243.   /* Reference to literal section.  */
  244.   HOWTO (R_MIPS_LITERAL,    /* type */
  245.      0,            /* rightshift */
  246.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  247.      16,            /* bitsize */
  248.      false,            /* pc_relative */
  249.      0,            /* bitpos */
  250.      complain_overflow_signed, /* complain_on_overflow */
  251.      mips_elf_gprel16_reloc, /* special_function */
  252.      "R_MIPS_LITERAL",    /* name */
  253.      true,            /* partial_inplace */
  254.      0xffff,        /* src_mask */
  255.      0xffff,        /* dst_mask */
  256.      false),        /* pcrel_offset */
  257.  
  258.   /* Reference to global offset table.  */
  259.   /* FIXME: This is not handled correctly.  */
  260.   HOWTO (R_MIPS_GOT16,        /* type */
  261.      0,            /* rightshift */
  262.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  263.      16,            /* bitsize */
  264.      false,            /* pc_relative */
  265.      0,            /* bitpos */
  266.      complain_overflow_signed, /* complain_on_overflow */
  267.      mips_elf_got16_reloc,    /* special_function */
  268.      "R_MIPS_GOT16",    /* name */
  269.      false,            /* partial_inplace */
  270.      0,            /* src_mask */
  271.      0xffff,        /* dst_mask */
  272.      false),        /* pcrel_offset */
  273.  
  274.   /* 16 bit PC relative reference.  */
  275.   HOWTO (R_MIPS_PC16,        /* type */
  276.      0,            /* rightshift */
  277.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  278.      16,            /* bitsize */
  279.      true,            /* pc_relative */
  280.      0,            /* bitpos */
  281.      complain_overflow_signed, /* complain_on_overflow */
  282.      bfd_elf_generic_reloc,    /* special_function */
  283.      "R_MIPS_PC16",        /* name */
  284.      true,            /* partial_inplace */
  285.      0xffff,        /* src_mask */
  286.      0xffff,        /* dst_mask */
  287.      false),        /* pcrel_offset */
  288.  
  289.   /* 16 bit call through global offset table.  */
  290.   /* FIXME: This is not handled correctly.  */
  291.   HOWTO (R_MIPS_CALL16,        /* type */
  292.      0,            /* rightshift */
  293.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  294.      16,            /* bitsize */
  295.      false,            /* pc_relative */
  296.      0,            /* bitpos */
  297.      complain_overflow_signed, /* complain_on_overflow */
  298.      bfd_elf_generic_reloc,    /* special_function */
  299.      "R_MIPS_CALL16",    /* name */
  300.      false,            /* partial_inplace */
  301.      0,            /* src_mask */
  302.      0xffff,        /* dst_mask */
  303.      false),        /* pcrel_offset */
  304.  
  305.   /* 32 bit GP relative reference.  */
  306.   /* FIXME: This is not handled correctly.  */
  307.   HOWTO (R_MIPS_GPREL32,    /* type */
  308.      0,            /* rightshift */
  309.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  310.      32,            /* bitsize */
  311.      false,            /* pc_relative */
  312.      0,            /* bitpos */
  313.      complain_overflow_bitfield, /* complain_on_overflow */
  314.      bfd_elf_generic_reloc,    /* special_function */
  315.      "R_MIPS_GPREL32",    /* name */
  316.      true,            /* partial_inplace */
  317.      0xffffffff,        /* src_mask */
  318.      0xffffffff,        /* dst_mask */
  319.      false)            /* pcrel_offset */
  320. };
  321.  
  322. /* Do a R_MIPS_HI16 relocation.  This has to be done in combination
  323.    with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
  324.    the HI16.  Here we just save the information we need; we do the
  325.    actual relocation when we see the LO16.  MIPS ELF requires that the
  326.    LO16 immediately follow the HI16, so this ought to work.  */
  327.  
  328. static bfd_byte *mips_hi16_addr;
  329. static bfd_vma mips_hi16_addend;
  330.  
  331. static bfd_reloc_status_type
  332. mips_elf_hi16_reloc (abfd,
  333.              reloc_entry,
  334.              symbol,
  335.              data,
  336.              input_section,
  337.              output_bfd,
  338.              error_message)
  339.      bfd *abfd;
  340.      arelent *reloc_entry;
  341.      asymbol *symbol;
  342.      PTR data;
  343.      asection *input_section;
  344.      bfd *output_bfd;
  345.      char **error_message;
  346. {
  347.   bfd_reloc_status_type ret;
  348.   bfd_vma relocation;
  349.  
  350.   /* If we're relocating, and this an external symbol, we don't want
  351.      to change anything.  */
  352.   if (output_bfd != (bfd *) NULL
  353.       && (symbol->flags & BSF_SECTION_SYM) == 0
  354.       && reloc_entry->addend == 0)
  355.     {
  356.       reloc_entry->address += input_section->output_offset;
  357.       return bfd_reloc_ok;
  358.     }
  359.  
  360.   /* FIXME: The symbol _gp_disp requires special handling, which we do
  361.      not do.  */
  362.   if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
  363.     abort ();
  364.  
  365.   ret = bfd_reloc_ok;
  366.   if (bfd_is_und_section (symbol->section)
  367.       && output_bfd == (bfd *) NULL)
  368.     ret = bfd_reloc_undefined;
  369.  
  370.   if (bfd_is_com_section (symbol->section))
  371.     relocation = 0;
  372.   else
  373.     relocation = symbol->value;
  374.  
  375.   relocation += symbol->section->output_section->vma;
  376.   relocation += symbol->section->output_offset;
  377.   relocation += reloc_entry->addend;
  378.  
  379.   if (reloc_entry->address > input_section->_cooked_size)
  380.     return bfd_reloc_outofrange;
  381.  
  382.   /* Save the information, and let LO16 do the actual relocation.  */
  383.   mips_hi16_addr = (bfd_byte *) data + reloc_entry->address;
  384.   mips_hi16_addend = relocation;
  385.  
  386.   if (output_bfd != (bfd *) NULL)
  387.     reloc_entry->address += input_section->output_offset;
  388.  
  389.   return ret;
  390. }
  391.  
  392. /* Do a R_MIPS_LO16 relocation.  This is a straightforward 16 bit
  393.    inplace relocation; this function exists in order to do the
  394.    R_MIPS_HI16 relocation described above.  */
  395.  
  396. static bfd_reloc_status_type
  397. mips_elf_lo16_reloc (abfd,
  398.              reloc_entry,
  399.              symbol,
  400.              data,
  401.              input_section,
  402.              output_bfd,
  403.              error_message)
  404.      bfd *abfd;
  405.      arelent *reloc_entry;
  406.      asymbol *symbol;
  407.      PTR data;
  408.      asection *input_section;
  409.      bfd *output_bfd;
  410.      char **error_message;
  411. {
  412.   /* FIXME: The symbol _gp_disp requires special handling, which we do
  413.      not do.  */
  414.   if (output_bfd == (bfd *) NULL
  415.       && strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
  416.     abort ();
  417.  
  418.   if (mips_hi16_addr != (bfd_byte *) NULL)
  419.     {
  420.       unsigned long insn;
  421.       unsigned long val;
  422.       unsigned long vallo;
  423.  
  424.       /* Do the HI16 relocation.  Note that we actually don't need to
  425.      know anything about the LO16 itself, except where to find the
  426.      low 16 bits of the addend needed by the LO16.  */
  427.       insn = bfd_get_32 (abfd, mips_hi16_addr);
  428.       vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
  429.            & 0xffff);
  430.       val = ((insn & 0xffff) << 16) + vallo;
  431.       val += mips_hi16_addend;
  432.  
  433.       /* The low order 16 bits are always treated as a signed value.
  434.      Therefore, a negative value in the low order bits requires an
  435.      adjustment in the high order bits.  We need to make this
  436.      adjustment in two ways: once for the bits we took from the
  437.      data, and once for the bits we are putting back in to the
  438.      data.  */
  439.       if ((vallo & 0x8000) != 0)
  440.     val -= 0x10000;
  441.       if ((val & 0x8000) != 0)
  442.     val += 0x10000;
  443.  
  444.       insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
  445.       bfd_put_32 (abfd, insn, mips_hi16_addr);
  446.  
  447.       mips_hi16_addr = (bfd_byte *) NULL;
  448.     }
  449.  
  450.   /* Now do the LO16 reloc in the usual way.  */
  451.   return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  452.                 input_section, output_bfd, error_message);
  453. }
  454.  
  455. /* Do a R_MIPS_GOT16 reloc.  This is a reloc against the global offset
  456.    table used for PIC code.  If the symbol is an external symbol, the
  457.    instruction is modified to contain the offset of the appropriate
  458.    entry in the global offset table.  If the symbol is a section
  459.    symbol, the next reloc is a R_MIPS_LO16 reloc.  The two 16 bit
  460.    addends are combined to form the real addend against the section
  461.    symbol; the GOT16 is modified to contain the offset of an entry in
  462.    the global offset table, and the LO16 is modified to offset it
  463.    appropriately.  Thus an offset larger than 16 bits requires a
  464.    modified value in the global offset table.
  465.  
  466.    This implementation suffices for the assembler, but the linker does
  467.    not yet know how to create global offset tables.  */
  468.  
  469. static bfd_reloc_status_type
  470. mips_elf_got16_reloc (abfd,
  471.               reloc_entry,
  472.               symbol,
  473.               data,
  474.               input_section,
  475.               output_bfd,
  476.               error_message)
  477.      bfd *abfd;
  478.      arelent *reloc_entry;
  479.      asymbol *symbol;
  480.      PTR data;
  481.      asection *input_section;
  482.      bfd *output_bfd;
  483.      char **error_message;
  484. {
  485.   /* If we're relocating, and this an external symbol, we don't want
  486.      to change anything.  */
  487.   if (output_bfd != (bfd *) NULL
  488.       && (symbol->flags & BSF_SECTION_SYM) == 0
  489.       && reloc_entry->addend == 0)
  490.     {
  491.       reloc_entry->address += input_section->output_offset;
  492.       return bfd_reloc_ok;
  493.     }
  494.  
  495.   /* If we're relocating, and this is a local symbol, we can handle it
  496.      just like HI16.  */
  497.   if (output_bfd != (bfd *) NULL
  498.       && (symbol->flags & BSF_SECTION_SYM) != 0)
  499.     return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
  500.                 input_section, output_bfd, error_message);
  501.  
  502.   abort ();
  503. }
  504.  
  505. /* Do a R_MIPS_GPREL16 relocation.  This is a 16 bit value which must
  506.    become the offset from the gp register.  This function also handles
  507.    R_MIPS_LITERAL relocations, although those can be handled more
  508.    cleverly because the entries in the .lit8 and .lit4 sections can be
  509.    merged.  */
  510.  
  511. static bfd_reloc_status_type
  512. mips_elf_gprel16_reloc (abfd,
  513.             reloc_entry,
  514.             symbol,
  515.             data,
  516.             input_section,
  517.             output_bfd,
  518.             error_message)
  519.      bfd *abfd;
  520.      arelent *reloc_entry;
  521.      asymbol *symbol;
  522.      PTR data;
  523.      asection *input_section;
  524.      bfd *output_bfd;
  525.      char **error_message;
  526. {
  527.   boolean relocateable;
  528.   bfd_vma relocation;
  529.   unsigned long val;
  530.   unsigned long insn;
  531.  
  532.   /* If we're relocating, and this is an external symbol with no
  533.      addend, we don't want to change anything.  We will only have an
  534.      addend if this is a newly created reloc, not read from an ELF
  535.      file.  */
  536.   if (output_bfd != (bfd *) NULL
  537.       && (symbol->flags & BSF_SECTION_SYM) == 0
  538.       && reloc_entry->addend == 0)
  539.     {
  540.       reloc_entry->address += input_section->output_offset;
  541.       return bfd_reloc_ok;
  542.     }
  543.  
  544.   if (output_bfd != (bfd *) NULL)
  545.     relocateable = true;
  546.   else
  547.     {
  548.       relocateable = false;
  549.       output_bfd = symbol->section->output_section->owner;
  550.     }
  551.  
  552.   if (bfd_is_und_section (symbol->section)
  553.       && relocateable == false)
  554.     return bfd_reloc_undefined;
  555.  
  556.   /* We have to figure out the gp value, so that we can adjust the
  557.      symbol value correctly.  We look up the symbol _gp in the output
  558.      BFD.  If we can't find it, we're stuck.  We cache it in the ELF
  559.      target data.  We don't need to adjust the symbol value for an
  560.      external symbol if we are producing relocateable output.  */
  561.   if (elf_gp (output_bfd) == 0
  562.       && (relocateable == false
  563.       || (symbol->flags & BSF_SECTION_SYM) != 0))
  564.     {
  565.       if (relocateable != false)
  566.     {
  567.       /* Make up a value.  */
  568.       elf_gp (output_bfd) =
  569.         symbol->section->output_section->vma + 0x4000;
  570.     }
  571.       else
  572.     {
  573.       unsigned int count;
  574.       asymbol **sym;
  575.       unsigned int i;
  576.  
  577.       count = bfd_get_symcount (output_bfd);
  578.       sym = bfd_get_outsymbols (output_bfd);
  579.  
  580.       if (sym == (asymbol **) NULL)
  581.         i = count;
  582.       else
  583.         {
  584.           for (i = 0; i < count; i++, sym++)
  585.         {
  586.           register CONST char *name;
  587.  
  588.           name = bfd_asymbol_name (*sym);
  589.           if (*name == '_' && strcmp (name, "_gp") == 0)
  590.             {
  591.               elf_gp (output_bfd) = bfd_asymbol_value (*sym);
  592.               break;
  593.             }
  594.         }
  595.         }
  596.  
  597.       if (i >= count)
  598.         {
  599.           /* Only get the error once.  */
  600.           elf_gp (output_bfd) = 4;
  601.           *error_message =
  602.         (char *) "GP relative relocation when _gp not defined";
  603.           return bfd_reloc_dangerous;
  604.         }
  605.     }
  606.     }
  607.  
  608.   if (bfd_is_com_section (symbol->section))
  609.     relocation = 0;
  610.   else
  611.     relocation = symbol->value;
  612.  
  613.   relocation += symbol->section->output_section->vma;
  614.   relocation += symbol->section->output_offset;
  615.  
  616.   if (reloc_entry->address > input_section->_cooked_size)
  617.     return bfd_reloc_outofrange;
  618.  
  619.   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  620.  
  621.   /* Set val to the offset into the section or symbol.  */
  622.   val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
  623.   if (val & 0x8000)
  624.     val -= 0x10000;
  625.  
  626.   /* Adjust val for the final section location and GP value.  If we
  627.      are producing relocateable output, we don't want to do this for
  628.      an external symbol.  */
  629.   if (relocateable == false
  630.       || (symbol->flags & BSF_SECTION_SYM) != 0)
  631.     val += relocation - elf_gp (output_bfd);
  632.  
  633.   insn = (insn &~ 0xffff) | (val & 0xffff);
  634.   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  635.  
  636.   if (relocateable != false)
  637.     reloc_entry->address += input_section->output_offset;
  638.  
  639.   /* Make sure it fit in 16 bits.  */
  640.   if (val >= 0x8000 && val < 0xffff8000)
  641.     return bfd_reloc_overflow;
  642.  
  643.   return bfd_reloc_ok;
  644. }
  645.  
  646. /* A mapping from BFD reloc types to MIPS ELF reloc types.  */
  647.  
  648. struct elf_reloc_map {
  649.   bfd_reloc_code_real_type bfd_reloc_val;
  650.   enum reloc_type elf_reloc_val;
  651. };
  652.  
  653. static CONST struct elf_reloc_map mips_reloc_map[] =
  654. {
  655.   { BFD_RELOC_NONE, R_MIPS_NONE, },
  656.   { BFD_RELOC_16, R_MIPS_16 },
  657.   { BFD_RELOC_32, R_MIPS_32 },
  658.   { BFD_RELOC_CTOR, R_MIPS_32 },
  659.   { BFD_RELOC_32_PCREL, R_MIPS_REL32 },
  660.   { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
  661.   { BFD_RELOC_HI16_S, R_MIPS_HI16 },
  662.   { BFD_RELOC_LO16, R_MIPS_LO16 },
  663.   { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
  664.   { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
  665.   { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
  666.   { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
  667.   { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
  668.   { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 }
  669. };
  670.  
  671. /* Given a BFD reloc type, return a howto structure.  */
  672.  
  673. static const struct reloc_howto_struct *
  674. bfd_elf32_bfd_reloc_type_lookup (abfd, code)
  675.      bfd *abfd;
  676.      bfd_reloc_code_real_type code;
  677. {
  678.   int i;
  679.  
  680.   for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
  681.     {
  682.       if (mips_reloc_map[i].bfd_reloc_val == code)
  683.     return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
  684.     }
  685.   return NULL;
  686. }
  687.  
  688. /* Given a MIPS reloc type, fill in an arelent structure.  */
  689.  
  690. static void
  691. mips_info_to_howto_rel (abfd, cache_ptr, dst)
  692.      bfd *abfd;
  693.      arelent *cache_ptr;
  694.      Elf32_Internal_Rel *dst;
  695. {
  696.   unsigned int r_type;
  697.  
  698.   r_type = ELF32_R_TYPE (dst->r_info);
  699.   BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
  700.   cache_ptr->howto = &elf_mips_howto_table[r_type];
  701.  
  702.   /* The addend for a GPREL16 or LITERAL relocation comes from the GP
  703.      value for the object file.  We get the addend now, rather than
  704.      when we do the relocation, because the symbol manipulations done
  705.      by the linker may cause us to lose track of the input BFD.  */
  706.   if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
  707.       && (r_type == (unsigned int) R_MIPS_GPREL16
  708.       || r_type == (unsigned int) R_MIPS_LITERAL))
  709.     cache_ptr->addend = elf_gp (abfd);
  710. }
  711.  
  712. /* A .reginfo section holds a single Elf32_RegInfo structure.  These
  713.    routines swap this structure in and out.  They are used outside of
  714.    BFD, so they are globally visible.  */
  715.  
  716. void
  717. bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
  718.      bfd *abfd;
  719.      const Elf32_External_RegInfo *ex;
  720.      Elf32_RegInfo *in;
  721. {
  722.   in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
  723.   in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
  724.   in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
  725.   in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
  726.   in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
  727.   in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
  728. }
  729.  
  730. void
  731. bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
  732.      bfd *abfd;
  733.      const Elf32_RegInfo *in;
  734.      Elf32_External_RegInfo *ex;
  735. {
  736.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
  737.         (bfd_byte *) ex->ri_gprmask);
  738.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
  739.         (bfd_byte *) ex->ri_cprmask[0]);
  740.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
  741.         (bfd_byte *) ex->ri_cprmask[1]);
  742.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
  743.         (bfd_byte *) ex->ri_cprmask[2]);
  744.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
  745.         (bfd_byte *) ex->ri_cprmask[3]);
  746.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
  747.         (bfd_byte *) ex->ri_gp_value);
  748. }
  749.  
  750. /* Swap an entry in a .gptab section.  Note that these routines rely
  751.    on the equivalence of the two elements of the union.  */
  752.  
  753. static void
  754. bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
  755.      bfd *abfd;
  756.      const Elf32_External_gptab *ex;
  757.      Elf32_gptab *in;
  758. {
  759.   in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
  760.   in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
  761. }
  762.  
  763. static void
  764. bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
  765.      bfd *abfd;
  766.      const Elf32_gptab *in;
  767.      Elf32_External_gptab *ex;
  768. {
  769.   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
  770.         ex->gt_entry.gt_g_value);
  771.   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
  772.         ex->gt_entry.gt_bytes);
  773. }
  774.  
  775. /* Determine whether a symbol is global for the purposes of splitting
  776.    the symbol table into global symbols and local symbols.  At least
  777.    on Irix 5, this split must be between section symbols and all other
  778.    symbols.  On most ELF targets the split is between static symbols
  779.    and externally visible symbols.  */
  780.  
  781. /*ARGSUSED*/
  782. static boolean
  783. mips_elf_sym_is_global (abfd, sym)
  784.      bfd *abfd;
  785.      asymbol *sym;
  786. {
  787.   return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
  788. }
  789.  
  790. /* Set the right machine number for a MIPS ELF file.  */
  791.  
  792. static boolean
  793. mips_elf_object_p (abfd)
  794.      bfd *abfd;
  795. {
  796.   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
  797.     {
  798.     default:
  799.     case E_MIPS_ARCH_1:
  800.       /* Just use the default, which was set in elfcode.h.  */
  801.       break;
  802.  
  803.     case E_MIPS_ARCH_2:
  804.       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
  805.       break;
  806.  
  807.     case E_MIPS_ARCH_3:
  808.       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
  809.       break;
  810.     }
  811.  
  812.   /* Irix 5 is broken.  Object file symbol tables are not always
  813.      sorted correctly such that local symbols precede global symbols,
  814.      and the sh_info field in the symbol table is not always right.
  815.      We try to quickly check whether the symbol table is broken for
  816.      this BFD, and, if it is, we set elf_bad_symtab in tdata.  */
  817.   if (elf_onesymtab (abfd) != 0)
  818.     {
  819.       Elf_Internal_Shdr *symtab_hdr;
  820.       Elf32_External_Sym esym;
  821.  
  822.       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  823.       if (bfd_seek (abfd,
  824.             (symtab_hdr->sh_offset
  825.              + symtab_hdr->sh_size
  826.              - sizeof (Elf32_External_Sym)),
  827.             SEEK_SET) != 0
  828.       || (bfd_read ((PTR) &esym, 1, sizeof (Elf32_External_Sym), abfd)
  829.           != sizeof (Elf32_External_Sym)))
  830.     return false;
  831.       if (ELF_ST_BIND (bfd_h_get_8 (abfd, (bfd_byte *) esym.st_info))
  832.       == STB_LOCAL)
  833.     elf_bad_symtab (abfd) = true;
  834.     }
  835.  
  836.   return true;
  837. }
  838.  
  839. /* The final processing done just before writing out a MIPS ELF object
  840.    file.  This gets the MIPS architecture right based on the machine
  841.    number.  */
  842.  
  843. /*ARGSUSED*/
  844. static void
  845. mips_elf_final_write_processing (abfd, linker)
  846.      bfd *abfd;
  847.      boolean linker;
  848. {
  849.   unsigned long val;
  850.   unsigned int i;
  851.   Elf_Internal_Shdr **hdrpp;
  852.  
  853.   switch (bfd_get_mach (abfd))
  854.     {
  855.     case 3000:
  856.       val = E_MIPS_ARCH_1;
  857.       break;
  858.  
  859.     case 6000:
  860.       val = E_MIPS_ARCH_2;
  861.       break;
  862.  
  863.     case 4000:
  864.       val = E_MIPS_ARCH_3;
  865.       break;
  866.  
  867.     default:
  868.       return;
  869.     }
  870.  
  871.   elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
  872.   elf_elfheader (abfd)->e_flags |= val;
  873.  
  874.   /* Set the sh_info field for .gptab sections.  */
  875.   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
  876.        i < elf_elfheader (abfd)->e_shnum;
  877.        i++, hdrpp++)
  878.     {
  879.       if ((*hdrpp)->sh_type == SHT_MIPS_GPTAB)
  880.     {
  881.       const char *name;
  882.       asection *sec;
  883.  
  884.       BFD_ASSERT ((*hdrpp)->rawdata != NULL);
  885.       name = ((asection *) (*hdrpp)->rawdata)->name;
  886.       BFD_ASSERT (name != NULL
  887.               && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
  888.       sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
  889.       BFD_ASSERT (sec != NULL);
  890.       (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
  891.     }
  892.     }
  893. }
  894.  
  895. /* Handle a MIPS specific section when reading an object file.  This
  896.    is called when elfcode.h finds a section with an unknown type.
  897.    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
  898.    how to.  */
  899.  
  900. static boolean
  901. mips_elf_section_from_shdr (abfd, hdr, name)
  902.      bfd *abfd;
  903.      Elf32_Internal_Shdr *hdr;
  904.      char *name;
  905. {
  906.   asection *newsect;
  907.  
  908.   /* There ought to be a place to keep ELF backend specific flags, but
  909.      at the moment there isn't one.  We just keep track of the
  910.      sections by their name, instead.  Fortunately, the ABI gives
  911.      suggested names for all the MIPS specific sections, so we will
  912.      probably get away with this.  */
  913.   switch (hdr->sh_type)
  914.     {
  915.     case SHT_MIPS_LIBLIST:
  916.       if (strcmp (name, ".liblist") != 0)
  917.     return false;
  918.       break;
  919.     case SHT_MIPS_CONFLICT:
  920.       if (strcmp (name, ".conflict") != 0)
  921.     return false;
  922.       break;
  923.     case SHT_MIPS_GPTAB:
  924.       if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
  925.     return false;
  926.       break;
  927.     case SHT_MIPS_UCODE:
  928.       if (strcmp (name, ".ucode") != 0)
  929.     return false;
  930.       break;
  931.     case SHT_MIPS_DEBUG:
  932.       if (strcmp (name, ".mdebug") != 0)
  933.     return false;
  934.       break;
  935.     case SHT_MIPS_REGINFO:
  936.       if (strcmp (name, ".reginfo") != 0
  937.       || hdr->sh_size != sizeof (Elf32_External_RegInfo))
  938.     return false;
  939.       break;
  940.     case SHT_MIPS_OPTIONS:
  941.       if (strcmp (name, ".options") != 0)
  942.     return false;
  943.       break;
  944.     default:
  945.       return false;
  946.     }
  947.  
  948.   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
  949.     return false;
  950.   newsect = (asection *) hdr->rawdata;
  951.  
  952.   if (hdr->sh_type == SHT_MIPS_DEBUG)
  953.     {
  954.       if (! bfd_set_section_flags (abfd, newsect,
  955.                    (bfd_get_section_flags (abfd, newsect)
  956.                     | SEC_DEBUGGING)))
  957.     return false;
  958.     }
  959.  
  960.   /* FIXME: We should record sh_info for a .gptab section.  */
  961.  
  962.   /* For a .reginfo section, set the gp value in the tdata information
  963.      from the contents of this section.  We need the gp value while
  964.      processing relocs, so we just get it now.  */
  965.   if (hdr->sh_type == SHT_MIPS_REGINFO)
  966.     {
  967.       Elf32_External_RegInfo ext;
  968.       Elf32_RegInfo s;
  969.  
  970.       if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
  971.                       (file_ptr) 0, sizeof ext))
  972.     return false;
  973.       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
  974.       elf_gp (abfd) = s.ri_gp_value;
  975.     }
  976.  
  977.   return true;
  978. }
  979.  
  980. /* Set the correct type for a MIPS ELF section.  We do this by the
  981.    section name, which is a hack, but ought to work.  */
  982.  
  983. static boolean
  984. mips_elf_fake_sections (abfd, hdr, sec)
  985.      bfd *abfd;
  986.      Elf32_Internal_Shdr *hdr;
  987.      asection *sec;
  988. {
  989.   register const char *name;
  990.  
  991.   name = bfd_get_section_name (abfd, sec);
  992.  
  993.   if (strcmp (name, ".liblist") == 0)
  994.     {
  995.       hdr->sh_type = SHT_MIPS_LIBLIST;
  996.       hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
  997.       /* FIXME: Set the sh_link field.  */
  998.     }
  999.   else if (strcmp (name, ".conflict") == 0)
  1000.     hdr->sh_type = SHT_MIPS_CONFLICT;
  1001.   else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
  1002.     {
  1003.       hdr->sh_type = SHT_MIPS_GPTAB;
  1004.       hdr->sh_entsize = sizeof (Elf32_External_gptab);
  1005.       /* The sh_info field is set in mips_elf_final_write_processing.  */
  1006.     }
  1007.   else if (strcmp (name, ".ucode") == 0)
  1008.     hdr->sh_type = SHT_MIPS_UCODE;
  1009.   else if (strcmp (name, ".mdebug") == 0)
  1010.     {
  1011.       hdr->sh_type = SHT_MIPS_DEBUG;
  1012.       hdr->sh_entsize = 1;
  1013.     }
  1014.   else if (strcmp (name, ".reginfo") == 0)
  1015.     {
  1016.       hdr->sh_type = SHT_MIPS_REGINFO;
  1017.       hdr->sh_entsize = 1;
  1018.  
  1019.       /* Force the section size to the correct value, even if the
  1020.      linker thinks it is larger.  The link routine below will only
  1021.      write out this much data for .reginfo.  */
  1022.       hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
  1023.     }
  1024.   else if (strcmp (name, ".options") == 0)
  1025.     {
  1026.       hdr->sh_type = SHT_MIPS_OPTIONS;
  1027.       hdr->sh_entsize = 1;
  1028.     }
  1029.  
  1030.   return true;
  1031. }
  1032.  
  1033. /* Given a BFD section, try to locate the corresponding ELF section
  1034.    index.  */
  1035.  
  1036. static boolean
  1037. mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
  1038.      bfd *abfd;
  1039.      Elf32_Internal_Shdr *hdr;
  1040.      asection *sec;
  1041.      int *retval;
  1042. {
  1043.   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
  1044.     {
  1045.       *retval = SHN_MIPS_SCOMMON;
  1046.       return true;
  1047.     }
  1048.   if ((asection *) hdr->rawdata == sec)
  1049.     return true;
  1050.   return false;
  1051. }
  1052.  
  1053. /* Work over a section just before writing it out.  We update the GP
  1054.    value in the .reginfo section based on the value we are using.
  1055.    FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
  1056.    name; there has to be a better way.  */
  1057.  
  1058. static boolean
  1059. mips_elf_section_processing (abfd, hdr)
  1060.      bfd *abfd;
  1061.      Elf32_Internal_Shdr *hdr;
  1062. {
  1063.   if (hdr->sh_type == SHT_MIPS_REGINFO)
  1064.     {
  1065.       bfd_byte buf[4];
  1066.  
  1067.       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
  1068.       BFD_ASSERT (hdr->contents == NULL);
  1069.  
  1070.       if (bfd_seek (abfd,
  1071.             hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
  1072.             SEEK_SET) == -1)
  1073.     return false;
  1074.       bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
  1075.       if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
  1076.     return false;
  1077.     }
  1078.  
  1079.   if (hdr->rawdata != NULL)
  1080.     {
  1081.       const char *name = ((asection *) hdr->rawdata)->name;
  1082.  
  1083.       if (strcmp (name, ".sdata") == 0)
  1084.     {
  1085.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  1086.       hdr->sh_type = SHT_PROGBITS;
  1087.     }
  1088.       else if (strcmp (name, ".sbss") == 0)
  1089.     {
  1090.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  1091.       hdr->sh_type = SHT_NOBITS;
  1092.     }
  1093.       else if (strcmp (name, ".lit8") == 0
  1094.            || strcmp (name, ".lit4") == 0)
  1095.     {
  1096.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  1097.       hdr->sh_type = SHT_PROGBITS;
  1098.     }
  1099.     }
  1100.  
  1101.   return true;
  1102. }
  1103.  
  1104. /* MIPS ELF uses two common sections.  One is the usual one, and the
  1105.    other is for small objects.  All the small objects are kept
  1106.    together, and then referenced via the gp pointer, which yields
  1107.    faster assembler code.  This is what we use for the small common
  1108.    section.  This approach is copied from ecoff.c.  */
  1109. static asection mips_elf_scom_section;
  1110. static asymbol mips_elf_scom_symbol;
  1111. static asymbol *mips_elf_scom_symbol_ptr;
  1112.  
  1113. /* MIPS ELF also uses an acommon section, which represents an
  1114.    allocated common symbol which may be overridden by a     
  1115.    definition in a shared library.  */
  1116. static asection mips_elf_acom_section;
  1117. static asymbol mips_elf_acom_symbol;
  1118. static asymbol *mips_elf_acom_symbol_ptr;
  1119.  
  1120. /* Handle the special MIPS section numbers that a symbol may use.  */
  1121.  
  1122. static void
  1123. mips_elf_symbol_processing (abfd, asym)
  1124.      bfd *abfd;
  1125.      asymbol *asym;
  1126. {
  1127.   elf_symbol_type *elfsym;
  1128.  
  1129.   elfsym = (elf_symbol_type *) asym;
  1130.   switch (elfsym->internal_elf_sym.st_shndx)
  1131.     {
  1132.     case SHN_MIPS_ACOMMON:
  1133.       /* This section is used in a dynamically linked executable file.
  1134.      It is an allocated common section.  The dynamic linker can
  1135.      either resolve these symbols to something in a shared
  1136.      library, or it can just leave them here.  For our purposes,
  1137.      we can consider these symbols to be in a new section.  */
  1138.       if (mips_elf_acom_section.name == NULL)
  1139.     {
  1140.       /* Initialize the acommon section.  */
  1141.       mips_elf_acom_section.name = ".acommon";
  1142.       mips_elf_acom_section.flags = SEC_NO_FLAGS;
  1143.       mips_elf_acom_section.output_section = &mips_elf_acom_section;
  1144.       mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
  1145.       mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
  1146.       mips_elf_acom_symbol.name = ".acommon";
  1147.       mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
  1148.       mips_elf_acom_symbol.section = &mips_elf_acom_section;
  1149.       mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
  1150.     }
  1151.       asym->section = &mips_elf_acom_section;
  1152.       break;
  1153.  
  1154.     case SHN_COMMON:
  1155.       /* Common symbols less than the GP size are automatically
  1156.      treated as SHN_MIPS_SCOMMON symbols.  */
  1157.       if (asym->value > elf_gp_size (abfd))
  1158.     break;
  1159.       /* Fall through.  */
  1160.     case SHN_MIPS_SCOMMON:
  1161.       if (mips_elf_scom_section.name == NULL)
  1162.     {
  1163.       /* Initialize the small common section.  */
  1164.       mips_elf_scom_section.name = ".scommon";
  1165.       mips_elf_scom_section.flags = SEC_IS_COMMON;
  1166.       mips_elf_scom_section.output_section = &mips_elf_scom_section;
  1167.       mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
  1168.       mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
  1169.       mips_elf_scom_symbol.name = ".scommon";
  1170.       mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
  1171.       mips_elf_scom_symbol.section = &mips_elf_scom_section;
  1172.       mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
  1173.     }
  1174.       asym->section = &mips_elf_scom_section;
  1175.       asym->value = elfsym->internal_elf_sym.st_size;
  1176.       break;
  1177.  
  1178.     case SHN_MIPS_SUNDEFINED:
  1179.       asym->section = bfd_und_section_ptr;
  1180.       break;
  1181.     }
  1182. }
  1183.  
  1184. /* Read ECOFF debugging information from a .mdebug section into a
  1185.    ecoff_debug_info structure.  */
  1186.  
  1187. static boolean
  1188. mips_elf_read_ecoff_info (abfd, section, debug)
  1189.      bfd *abfd;
  1190.      asection *section;
  1191.      struct ecoff_debug_info *debug;
  1192. {
  1193.   HDRR *symhdr;
  1194.   const struct ecoff_debug_swap *swap;
  1195.   char *ext_hdr = NULL;
  1196.  
  1197.   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1198.  
  1199.   ext_hdr = (char *) malloc (swap->external_hdr_size);
  1200.   if (ext_hdr == NULL && swap->external_hdr_size != 0)
  1201.     {
  1202.       bfd_set_error (bfd_error_no_memory);
  1203.       goto error_return;
  1204.     }
  1205.  
  1206.   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
  1207.                 swap->external_hdr_size)
  1208.       == false)
  1209.     goto error_return;
  1210.  
  1211.   symhdr = &debug->symbolic_header;
  1212.   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
  1213.  
  1214.   /* The symbolic header contains absolute file offsets and sizes to
  1215.      read.  */
  1216. #define READ(ptr, offset, count, size, type)                \
  1217.   if (symhdr->count == 0)                        \
  1218.     debug->ptr = NULL;                            \
  1219.   else                                    \
  1220.     {                                    \
  1221.       debug->ptr = (type) malloc (size * symhdr->count);        \
  1222.       if (debug->ptr == NULL)                        \
  1223.     {                                \
  1224.       bfd_set_error (bfd_error_no_memory);                \
  1225.       goto error_return;                        \
  1226.     }                                \
  1227.       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0    \
  1228.       || (bfd_read (debug->ptr, size, symhdr->count,        \
  1229.             abfd) != size * symhdr->count))            \
  1230.     goto error_return;                        \
  1231.     }
  1232.  
  1233.   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
  1234.   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
  1235.   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
  1236.   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
  1237.   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
  1238.   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
  1239.     union aux_ext *);
  1240.   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
  1241.   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
  1242.   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
  1243.   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
  1244.   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
  1245. #undef READ
  1246.  
  1247.   debug->fdr = NULL;
  1248.   debug->adjust = NULL;
  1249.  
  1250.   return true;
  1251.  
  1252.  error_return:
  1253.   if (ext_hdr != NULL)
  1254.     free (ext_hdr);
  1255.   if (debug->line != NULL)
  1256.     free (debug->line);
  1257.   if (debug->external_dnr != NULL)
  1258.     free (debug->external_dnr);
  1259.   if (debug->external_pdr != NULL)
  1260.     free (debug->external_pdr);
  1261.   if (debug->external_sym != NULL)
  1262.     free (debug->external_sym);
  1263.   if (debug->external_opt != NULL)
  1264.     free (debug->external_opt);
  1265.   if (debug->external_aux != NULL)
  1266.     free (debug->external_aux);
  1267.   if (debug->ss != NULL)
  1268.     free (debug->ss);
  1269.   if (debug->ssext != NULL)
  1270.     free (debug->ssext);
  1271.   if (debug->external_fdr != NULL)
  1272.     free (debug->external_fdr);
  1273.   if (debug->external_rfd != NULL)
  1274.     free (debug->external_rfd);
  1275.   if (debug->external_ext != NULL)
  1276.     free (debug->external_ext);
  1277.   return false;
  1278. }
  1279.  
  1280. /* The MIPS ELF linker needs additional information for each symbol in
  1281.    the global hash table.  */
  1282.  
  1283. struct mips_elf_link_hash_entry
  1284. {
  1285.   struct elf_link_hash_entry root;
  1286.  
  1287.   /* External symbol information.  */
  1288.   EXTR esym;
  1289. };
  1290.  
  1291. /* MIPS ELF linker hash table.  */
  1292.  
  1293. struct mips_elf_link_hash_table
  1294. {
  1295.   struct elf_link_hash_table root;
  1296. };
  1297.  
  1298. /* Look up an entry in a MIPS ELF linker hash table.  */
  1299.  
  1300. #define mips_elf_link_hash_lookup(table, string, create, copy, follow)    \
  1301.   ((struct mips_elf_link_hash_entry *)                    \
  1302.    elf_link_hash_lookup (&(table)->root, (string), (create),        \
  1303.              (copy), (follow)))
  1304.  
  1305. /* Traverse a MIPS ELF linker hash table.  */
  1306.  
  1307. #define mips_elf_link_hash_traverse(table, func, info)            \
  1308.   (elf_link_hash_traverse                        \
  1309.    (&(table)->root,                            \
  1310.     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),    \
  1311.     (info)))
  1312.  
  1313. /* Get the MIPS ELF linker hash table from a link_info structure.  */
  1314.  
  1315. #define mips_elf_hash_table(p) \
  1316.   ((struct mips_elf_link_hash_table *) ((p)->hash))
  1317.  
  1318. static boolean mips_elf_output_extsym
  1319.   PARAMS ((struct mips_elf_link_hash_entry *, PTR));
  1320.  
  1321. /* Create an entry in a MIPS ELF linker hash table.  */
  1322.  
  1323. static struct bfd_hash_entry *
  1324. mips_elf_link_hash_newfunc (entry, table, string)
  1325.      struct bfd_hash_entry *entry;
  1326.      struct bfd_hash_table *table;
  1327.      const char *string;
  1328. {
  1329.   struct mips_elf_link_hash_entry *ret =
  1330.     (struct mips_elf_link_hash_entry *) entry;
  1331.  
  1332.   /* Allocate the structure if it has not already been allocated by a
  1333.      subclass.  */
  1334.   if (ret == (struct mips_elf_link_hash_entry *) NULL)
  1335.     ret = ((struct mips_elf_link_hash_entry *)
  1336.        bfd_hash_allocate (table,
  1337.                   sizeof (struct mips_elf_link_hash_entry)));
  1338.   if (ret == (struct mips_elf_link_hash_entry *) NULL)
  1339.     {
  1340.       bfd_set_error (bfd_error_no_memory);
  1341.       return (struct bfd_hash_entry *) ret;
  1342.     }
  1343.  
  1344.   /* Call the allocation method of the superclass.  */
  1345.   ret = ((struct mips_elf_link_hash_entry *)
  1346.      _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  1347.                      table, string));
  1348.   if (ret != (struct mips_elf_link_hash_entry *) NULL)
  1349.     {
  1350.       /* Set local fields.  */
  1351.       memset (&ret->esym, 0, sizeof (EXTR));
  1352.       /* We use -2 as a marker to indicate that the information has
  1353.      not been set.  -1 means there is no associated ifd.  */
  1354.       ret->esym.ifd = -2;
  1355.     }
  1356.  
  1357.   return (struct bfd_hash_entry *) ret;
  1358. }
  1359.  
  1360. /* Create a MIPS ELF linker hash table.  */
  1361.  
  1362. static struct bfd_link_hash_table *
  1363. mips_elf_link_hash_table_create (abfd)
  1364.      bfd *abfd;
  1365. {
  1366.   struct mips_elf_link_hash_table *ret;
  1367.  
  1368.   ret = ((struct mips_elf_link_hash_table *)
  1369.      bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
  1370.   if (ret == (struct mips_elf_link_hash_table *) NULL)
  1371.     {
  1372.       bfd_set_error (bfd_error_no_memory);
  1373.       return NULL;
  1374.     }
  1375.  
  1376.   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
  1377.                        mips_elf_link_hash_newfunc))
  1378.     {
  1379.       bfd_release (abfd, ret);
  1380.       return NULL;
  1381.     }
  1382.  
  1383.   return &ret->root.root;
  1384. }
  1385.  
  1386. /* Hook called by the linker routine which adds symbols from an object
  1387.    file.  We must handle the special MIPS section numbers here.  */
  1388.  
  1389. /*ARGSUSED*/
  1390. static boolean
  1391. mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
  1392.      bfd *abfd;
  1393.      struct bfd_link_info *info;
  1394.      const Elf_Internal_Sym *sym;
  1395.      const char **namep;
  1396.      flagword *flagsp;
  1397.      asection **secp;
  1398.      bfd_vma *valp;
  1399. {
  1400.   switch (sym->st_shndx)
  1401.     {
  1402.     case SHN_COMMON:
  1403.       /* Common symbols less than the GP size are automatically
  1404.      treated as SHN_MIPS_SCOMMON symbols.  */
  1405.       if (sym->st_size > elf_gp_size (abfd))
  1406.     break;
  1407.       /* Fall through.  */
  1408.     case SHN_MIPS_SCOMMON:
  1409.       *secp = bfd_make_section_old_way (abfd, ".scommon");
  1410.       (*secp)->flags |= SEC_IS_COMMON;
  1411.       *valp = sym->st_size;
  1412.       break;
  1413.  
  1414.     case SHN_MIPS_SUNDEFINED:
  1415.       *secp = bfd_und_section_ptr;
  1416.       break;
  1417.     }
  1418.  
  1419.   return true;
  1420. }
  1421.  
  1422. /* Structure used to pass information to mips_elf_output_extsym.  */
  1423.  
  1424. struct extsym_info
  1425. {
  1426.   bfd *abfd;
  1427.   struct bfd_link_info *info;
  1428.   struct ecoff_debug_info *debug;
  1429.   const struct ecoff_debug_swap *swap;
  1430.   boolean failed;
  1431. };
  1432.  
  1433. /* This routine is used to write out ECOFF debugging external symbol
  1434.    information.  It is called via mips_elf_link_hash_traverse.  The
  1435.    ECOFF external symbol information must match the ELF external
  1436.    symbol information.  Unfortunately, at this point we don't know
  1437.    whether a symbol is required by reloc information, so the two
  1438.    tables may wind up being different.  We must sort out the external
  1439.    symbol information before we can set the final size of the .mdebug
  1440.    section, and we must set the size of the .mdebug section before we
  1441.    can relocate any sections, and we can't know which symbols are
  1442.    required by relocation until we relocate the sections.
  1443.    Fortunately, it is relatively unlikely that any symbol will be
  1444.    stripped but required by a reloc.  In particular, it can not happen
  1445.    when generating a final executable.  */
  1446.  
  1447. static boolean
  1448. mips_elf_output_extsym (h, data)
  1449.      struct mips_elf_link_hash_entry *h;
  1450.      PTR data;
  1451. {
  1452.   struct extsym_info *einfo = (struct extsym_info *) data;
  1453.   boolean strip;
  1454.  
  1455.   if (h->root.indx == -2)
  1456.     strip = false;
  1457.   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  1458.         || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
  1459.        && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
  1460.        && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
  1461.     strip = true;
  1462.   else if (einfo->info->strip == strip_all
  1463.        || (einfo->info->strip == strip_some
  1464.            && bfd_hash_lookup (einfo->info->keep_hash,
  1465.                    h->root.root.root.string,
  1466.                    false, false) == NULL))
  1467.     strip = true;
  1468.   else
  1469.     strip = false;
  1470.  
  1471.   if (strip)
  1472.     return true;
  1473.  
  1474.   if (h->esym.ifd == -2)
  1475.     {
  1476.       h->esym.jmptbl = 0;
  1477.       h->esym.cobol_main = 0;
  1478.       h->esym.weakext = 0;
  1479.       h->esym.reserved = 0;
  1480.       h->esym.ifd = ifdNil;
  1481.       h->esym.asym.value = 0;
  1482.       h->esym.asym.st = stGlobal;
  1483.  
  1484.       if (h->root.root.type != bfd_link_hash_defined)
  1485.     h->esym.asym.sc = scAbs;
  1486.       else
  1487.     {
  1488.       asection *output_section;
  1489.       const char *name;
  1490.  
  1491.       output_section = h->root.root.u.def.section->output_section;
  1492.       name = bfd_section_name (output_section->owner, output_section);
  1493.     
  1494.       if (strcmp (name, ".text") == 0)
  1495.         h->esym.asym.sc = scText;
  1496.       else if (strcmp (name, ".data") == 0)
  1497.         h->esym.asym.sc = scData;
  1498.       else if (strcmp (name, ".sdata") == 0)
  1499.         h->esym.asym.sc = scSData;
  1500.       else if (strcmp (name, ".rodata") == 0
  1501.            || strcmp (name, ".rdata") == 0)
  1502.         h->esym.asym.sc = scRData;
  1503.       else if (strcmp (name, ".bss") == 0)
  1504.         h->esym.asym.sc = scBss;
  1505.       else if (strcmp (name, ".sbss") == 0)
  1506.         h->esym.asym.sc = scSBss;
  1507.       else if (strcmp (name, ".init") == 0)
  1508.         h->esym.asym.sc = scInit;
  1509.       else if (strcmp (name, ".fini") == 0)
  1510.         h->esym.asym.sc = scFini;
  1511.       else
  1512.         h->esym.asym.sc = scAbs;
  1513.     }
  1514.  
  1515.       h->esym.asym.reserved = 0;
  1516.       h->esym.asym.index = indexNil;
  1517.     }
  1518.  
  1519.  
  1520.   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
  1521.                       h->root.root.root.string,
  1522.                       &h->esym))
  1523.     {
  1524.       einfo->failed = true;
  1525.       return false;
  1526.     }
  1527.  
  1528.   return true;
  1529. }
  1530.  
  1531. /* A comparison routine used to sort .gptab entries.  */
  1532.  
  1533. static int
  1534. gptab_compare (p1, p2)
  1535.      const PTR p1;
  1536.      const PTR p2;
  1537. {
  1538.   const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
  1539.   const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
  1540.  
  1541.   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
  1542. }
  1543.  
  1544. /* We need to use a special link routine to handle the .reginfo and
  1545.    the .mdebug sections.  We need to merge all instances of these
  1546.    sections together, not write them all out sequentially.  */
  1547.  
  1548. static boolean
  1549. mips_elf_final_link (abfd, info)
  1550.      bfd *abfd;
  1551.      struct bfd_link_info *info;
  1552. {
  1553.   asection **secpp;
  1554.   asection *o;
  1555.   struct bfd_link_order *p;
  1556.   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
  1557.   Elf32_RegInfo reginfo;
  1558.   struct ecoff_debug_info debug;
  1559.   const struct ecoff_debug_swap *swap
  1560.     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1561.   HDRR *symhdr = &debug.symbolic_header;
  1562.   PTR mdebug_handle = NULL;
  1563.  
  1564.   /* Drop the .options section, since it has special semantics which I
  1565.      haven't bothered to figure out.  */
  1566.   for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
  1567.     {
  1568.       if (strcmp ((*secpp)->name, ".options") == 0)
  1569.     {
  1570.       for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
  1571.         if (p->type == bfd_indirect_link_order)
  1572.           p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
  1573.       (*secpp)->link_order_head = NULL;
  1574.       *secpp = (*secpp)->next;
  1575.       --abfd->section_count;
  1576.       break;
  1577.     }
  1578.     }
  1579.  
  1580.   /* Go through the sections and collect the .reginfo and .mdebug
  1581.      information.  */
  1582.   reginfo_sec = NULL;
  1583.   mdebug_sec = NULL;
  1584.   gptab_data_sec = NULL;
  1585.   gptab_bss_sec = NULL;
  1586.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  1587.     {
  1588.       if (strcmp (o->name, ".reginfo") == 0)
  1589.     {
  1590.       memset (®info, 0, sizeof reginfo);
  1591.  
  1592.       /* We have found the .reginfo section in the output file.
  1593.          Look through all the link_orders comprising it and merge
  1594.          the information together.  */
  1595.       for (p = o->link_order_head;
  1596.            p != (struct bfd_link_order *) NULL;
  1597.            p = p->next)
  1598.         {
  1599.           asection *input_section;
  1600.           bfd *input_bfd;
  1601.           Elf32_External_RegInfo ext;
  1602.           Elf32_RegInfo sub;
  1603.  
  1604.           if (p->type != bfd_indirect_link_order)
  1605.         {
  1606.           if (p->type == bfd_fill_link_order)
  1607.             continue;
  1608.           abort ();
  1609.         }
  1610.  
  1611.           input_section = p->u.indirect.section;
  1612.           input_bfd = input_section->owner;
  1613.           BFD_ASSERT (input_section->_raw_size
  1614.               == sizeof (Elf32_External_RegInfo));
  1615.           if (! bfd_get_section_contents (input_bfd, input_section,
  1616.                           (PTR) &ext,
  1617.                           (file_ptr) 0,
  1618.                           sizeof ext))
  1619.         return false;
  1620.  
  1621.           bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
  1622.  
  1623.           reginfo.ri_gprmask |= sub.ri_gprmask;
  1624.           reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
  1625.           reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
  1626.           reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
  1627.           reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
  1628.  
  1629.           /* ri_gp_value is set by the function
  1630.          mips_elf_section_processing when the section is
  1631.          finally written out.  */
  1632.  
  1633.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  1634.          elf_link_input_bfd ignores this section.  */
  1635.           input_section->flags &=~ SEC_HAS_CONTENTS;
  1636.         }
  1637.  
  1638.       /* Force the section size to the value we want.  */
  1639.       o->_raw_size = sizeof (Elf32_External_RegInfo);
  1640.  
  1641.       /* Skip this section later on (I don't think this currently
  1642.          matters, but someday it might).  */
  1643.       o->link_order_head = (struct bfd_link_order *) NULL;
  1644.  
  1645.       reginfo_sec = o;
  1646.     }
  1647.  
  1648.       if (strcmp (o->name, ".mdebug") == 0)
  1649.     {
  1650.       struct extsym_info einfo;
  1651.  
  1652.       /* We have found the .mdebug section in the output file.
  1653.          Look through all the link_orders comprising it and merge
  1654.          the information together.  */
  1655.       symhdr->magic = swap->sym_magic;
  1656.       /* FIXME: What should the version stamp be?  */
  1657.       symhdr->vstamp = 0;
  1658.       symhdr->ilineMax = 0;
  1659.       symhdr->cbLine = 0;
  1660.       symhdr->idnMax = 0;
  1661.       symhdr->ipdMax = 0;
  1662.       symhdr->isymMax = 0;
  1663.       symhdr->ioptMax = 0;
  1664.       symhdr->iauxMax = 0;
  1665.       symhdr->issMax = 0;
  1666.       symhdr->issExtMax = 0;
  1667.       symhdr->ifdMax = 0;
  1668.       symhdr->crfd = 0;
  1669.       symhdr->iextMax = 0;
  1670.  
  1671.       /* We accumulate the debugging information itself in the
  1672.          debug_info structure.  */
  1673.       debug.line = NULL;
  1674.       debug.external_dnr = NULL;
  1675.       debug.external_pdr = NULL;
  1676.       debug.external_sym = NULL;
  1677.       debug.external_opt = NULL;
  1678.       debug.external_aux = NULL;
  1679.       debug.ss = NULL;
  1680.       debug.ssext = debug.ssext_end = NULL;
  1681.       debug.external_fdr = NULL;
  1682.       debug.external_rfd = NULL;
  1683.       debug.external_ext = debug.external_ext_end = NULL;
  1684.  
  1685.       mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
  1686.       if (mdebug_handle == (PTR) NULL)
  1687.         return false;
  1688.  
  1689.       for (p = o->link_order_head;
  1690.            p != (struct bfd_link_order *) NULL;
  1691.            p = p->next)
  1692.         {
  1693.           asection *input_section;
  1694.           bfd *input_bfd;
  1695.           const struct ecoff_debug_swap *input_swap;
  1696.           struct ecoff_debug_info input_debug;
  1697.           char *eraw_src;
  1698.           char *eraw_end;
  1699.  
  1700.           if (p->type != bfd_indirect_link_order)
  1701.         {
  1702.           if (p->type == bfd_fill_link_order)
  1703.             continue;
  1704.           abort ();
  1705.         }
  1706.  
  1707.           input_section = p->u.indirect.section;
  1708.           input_bfd = input_section->owner;
  1709.  
  1710.           if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
  1711.           || (get_elf_backend_data (input_bfd)
  1712.               ->elf_backend_ecoff_debug_swap) == NULL)
  1713.         {
  1714.           /* I don't know what a non MIPS ELF bfd would be
  1715.              doing with a .mdebug section, but I don't really
  1716.              want to deal with it.  */
  1717.           continue;
  1718.         }
  1719.  
  1720.           input_swap = (get_elf_backend_data (input_bfd)
  1721.                 ->elf_backend_ecoff_debug_swap);
  1722.  
  1723.           BFD_ASSERT (p->size == input_section->_raw_size);
  1724.  
  1725.           /* The ECOFF linking code expects that we have already
  1726.          read in the debugging information and set up an
  1727.          ecoff_debug_info structure, so we do that now.  */
  1728.           if (! mips_elf_read_ecoff_info (input_bfd, input_section,
  1729.                           &input_debug))
  1730.         return false;
  1731.  
  1732.           if (! (bfd_ecoff_debug_accumulate
  1733.              (mdebug_handle, abfd, &debug, swap, input_bfd,
  1734.               &input_debug, input_swap, info)))
  1735.         return false;
  1736.  
  1737.           /* Loop through the external symbols.  For each one with
  1738.          interesting information, try to find the symbol in
  1739.          the linker global hash table and save the information
  1740.          for the output external symbols.  */
  1741.           eraw_src = input_debug.external_ext;
  1742.           eraw_end = (eraw_src
  1743.               + (input_debug.symbolic_header.iextMax
  1744.                  * input_swap->external_ext_size));
  1745.           for (;
  1746.            eraw_src < eraw_end;
  1747.            eraw_src += input_swap->external_ext_size)
  1748.         {
  1749.           EXTR ext;
  1750.           const char *name;
  1751.           struct mips_elf_link_hash_entry *h;
  1752.  
  1753.           (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
  1754.           if (ext.asym.sc == scNil
  1755.               || ext.asym.sc == scUndefined
  1756.               || ext.asym.sc == scSUndefined)
  1757.             continue;
  1758.  
  1759.           name = input_debug.ssext + ext.asym.iss;
  1760.           h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
  1761.                          name, false, false, true);
  1762.           if (h == NULL || h->esym.ifd != -2)
  1763.             continue;
  1764.  
  1765.           if (ext.ifd != -1)
  1766.             {
  1767.               BFD_ASSERT (ext.ifd
  1768.                   < input_debug.symbolic_header.ifdMax);
  1769.               ext.ifd = input_debug.ifdmap[ext.ifd];
  1770.             }
  1771.  
  1772.           h->esym = ext;
  1773.         }
  1774.  
  1775.           /* Free up the information we just read.  */
  1776.           free (input_debug.line);
  1777.           free (input_debug.external_dnr);
  1778.           free (input_debug.external_pdr);
  1779.           free (input_debug.external_sym);
  1780.           free (input_debug.external_opt);
  1781.           free (input_debug.external_aux);
  1782.           free (input_debug.ss);
  1783.           free (input_debug.ssext);
  1784.           free (input_debug.external_fdr);
  1785.           free (input_debug.external_rfd);
  1786.           free (input_debug.external_ext);
  1787.  
  1788.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  1789.          elf_link_input_bfd ignores this section.  */
  1790.           input_section->flags &=~ SEC_HAS_CONTENTS;
  1791.         }
  1792.  
  1793.       /* Build the external symbol information.  */
  1794.       einfo.abfd = abfd;
  1795.       einfo.info = info;
  1796.       einfo.debug = &debug;
  1797.       einfo.swap = swap;
  1798.       einfo.failed = false;
  1799.       mips_elf_link_hash_traverse (mips_elf_hash_table (info),
  1800.                        mips_elf_output_extsym,
  1801.                        (PTR) &einfo);
  1802.       if (einfo.failed)
  1803.         return false;
  1804.  
  1805.       /* Set the size of the .mdebug section.  */
  1806.       o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
  1807.  
  1808.       /* Skip this section later on (I don't think this currently
  1809.          matters, but someday it might).  */
  1810.       o->link_order_head = (struct bfd_link_order *) NULL;
  1811.  
  1812.       mdebug_sec = o;
  1813.     }
  1814.  
  1815.       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
  1816.     {
  1817.       const char *subname;
  1818.       unsigned int c;
  1819.       Elf32_gptab *tab;
  1820.       Elf32_External_gptab *ext_tab;
  1821.       unsigned int i;
  1822.  
  1823.       /* The .gptab.sdata and .gptab.sbss sections hold
  1824.          information describing how the small data area would
  1825.          change depending upon the -G switch.  These sections
  1826.          not used in executables files.  */
  1827.       if (! info->relocateable)
  1828.         {
  1829.           asection **secpp;
  1830.  
  1831.           for (p = o->link_order_head;
  1832.            p != (struct bfd_link_order *) NULL;
  1833.            p = p->next)
  1834.         {
  1835.           asection *input_section;
  1836.  
  1837.           if (p->type != bfd_indirect_link_order)
  1838.             {
  1839.               if (p->type == bfd_fill_link_order)
  1840.             continue;
  1841.               abort ();
  1842.             }
  1843.  
  1844.           input_section = p->u.indirect.section;
  1845.  
  1846.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  1847.              elf_link_input_bfd ignores this section.  */
  1848.           input_section->flags &=~ SEC_HAS_CONTENTS;
  1849.         }
  1850.  
  1851.           /* Skip this section later on (I don't think this
  1852.          currently matters, but someday it might).  */
  1853.           o->link_order_head = (struct bfd_link_order *) NULL;
  1854.  
  1855.           /* Really remove the section.  */
  1856.           for (secpp = &abfd->sections;
  1857.            *secpp != o;
  1858.            secpp = &(*secpp)->next)
  1859.         ;
  1860.           *secpp = (*secpp)->next;
  1861.           --abfd->section_count;
  1862.  
  1863.           continue;
  1864.         }
  1865.  
  1866.       /* There is one gptab for initialized data, and one for
  1867.          uninitialized data.  */
  1868.       if (strcmp (o->name, ".gptab.sdata") == 0)
  1869.         gptab_data_sec = o;
  1870.       else if (strcmp (o->name, ".gptab.sbss") == 0)
  1871.         gptab_bss_sec = o;
  1872.       else
  1873.         {
  1874.           bfd_set_error (bfd_error_nonrepresentable_section);
  1875.           return false;
  1876.         }
  1877.  
  1878.       /* The linker script always combines .gptab.data and
  1879.          .gptab.sdata into .gptab.sdata, and likewise for
  1880.          .gptab.bss and .gptab.sbss.  It is possible that there is
  1881.          no .sdata or .sbss section in the output file, in which
  1882.          case we must change the name of the output section.  */
  1883.       subname = o->name + sizeof ".gptab" - 1;
  1884.       if (bfd_get_section_by_name (abfd, subname) == NULL)
  1885.         {
  1886.           if (o == gptab_data_sec)
  1887.         o->name = ".gptab.data";
  1888.           else
  1889.         o->name = ".gptab.bss";
  1890.           subname = o->name + sizeof ".gptab" - 1;
  1891.           BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
  1892.         }
  1893.  
  1894.       /* Set up the first entry.  */
  1895.       c = 1;
  1896.       tab = (Elf32_gptab *) malloc (c * sizeof (Elf32_gptab));
  1897.       if (tab == NULL)
  1898.         {
  1899.           bfd_set_error (bfd_error_no_memory);
  1900.           return false;
  1901.         }
  1902.       tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
  1903.       tab[0].gt_header.gt_unused = 0;
  1904.  
  1905.       /* Combine the input sections.  */
  1906.       for (p = o->link_order_head;
  1907.            p != (struct bfd_link_order *) NULL;
  1908.            p = p->next)
  1909.         {
  1910.           asection *input_section;
  1911.           bfd *input_bfd;
  1912.           bfd_size_type size;
  1913.           unsigned long last;
  1914.           bfd_size_type gpentry;
  1915.  
  1916.           if (p->type != bfd_indirect_link_order)
  1917.         {
  1918.           if (p->type == bfd_fill_link_order)
  1919.             continue;
  1920.           abort ();
  1921.         }
  1922.  
  1923.           input_section = p->u.indirect.section;
  1924.           input_bfd = input_section->owner;
  1925.  
  1926.           /* Combine the gptab entries for this input section one
  1927.          by one.  We know that the input gptab entries are
  1928.          sorted by ascending -G value.  */
  1929.           size = bfd_section_size (input_bfd, input_section);
  1930.           last = 0;
  1931.           for (gpentry = sizeof (Elf32_External_gptab);
  1932.            gpentry < size;
  1933.            gpentry += sizeof (Elf32_External_gptab))
  1934.         {
  1935.           Elf32_External_gptab ext_gptab;
  1936.           Elf32_gptab int_gptab;
  1937.           unsigned long val;
  1938.           unsigned long add;
  1939.           boolean exact;
  1940.           unsigned int look;
  1941.  
  1942.           if (! (bfd_get_section_contents
  1943.              (input_bfd, input_section, (PTR) &ext_gptab,
  1944.               gpentry, sizeof (Elf32_External_gptab))))
  1945.             {
  1946.               free (tab);
  1947.               return false;
  1948.             }
  1949.  
  1950.           bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
  1951.                         &int_gptab);
  1952.           val = int_gptab.gt_entry.gt_g_value;
  1953.           add = int_gptab.gt_entry.gt_bytes - last;
  1954.  
  1955.           exact = false;
  1956.           for (look = 1; look < c; look++)
  1957.             {
  1958.               if (tab[look].gt_entry.gt_g_value >= val)
  1959.             tab[look].gt_entry.gt_bytes += add;
  1960.  
  1961.               if (tab[look].gt_entry.gt_g_value == val)
  1962.             exact = true;
  1963.             }
  1964.  
  1965.           if (! exact)
  1966.             {
  1967.               Elf32_gptab *new_tab;
  1968.               unsigned int max;
  1969.  
  1970.               /* We need a new table entry.  */
  1971.               new_tab = ((Elf32_gptab *)
  1972.                   realloc ((PTR) tab,
  1973.                        (c + 1) * sizeof (Elf32_gptab)));
  1974.               if (new_tab == NULL)
  1975.             {
  1976.               bfd_set_error (bfd_error_no_memory);
  1977.               free (tab);
  1978.               return false;
  1979.             }
  1980.               tab = new_tab;
  1981.               tab[c].gt_entry.gt_g_value = val;
  1982.               tab[c].gt_entry.gt_bytes = add;
  1983.  
  1984.               /* Merge in the size for the next smallest -G
  1985.              value, since that will be implied by this new
  1986.              value.  */
  1987.               max = 0;
  1988.               for (look = 1; look < c; look++)
  1989.             {
  1990.               if (tab[look].gt_entry.gt_g_value < val
  1991.                   && (max == 0
  1992.                   || (tab[look].gt_entry.gt_g_value
  1993.                       > tab[max].gt_entry.gt_g_value)))
  1994.                 max = look;
  1995.             }
  1996.               if (max != 0)
  1997.             tab[c].gt_entry.gt_bytes +=
  1998.               tab[max].gt_entry.gt_bytes;
  1999.  
  2000.               ++c;
  2001.             }
  2002.  
  2003.           last = int_gptab.gt_entry.gt_bytes;
  2004.         }
  2005.  
  2006.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  2007.          elf_link_input_bfd ignores this section.  */
  2008.           input_section->flags &=~ SEC_HAS_CONTENTS;
  2009.         }
  2010.  
  2011.       /* The table must be sorted by -G value.  */
  2012.       if (c > 2)
  2013.         qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
  2014.  
  2015.       /* Swap out the table.  */
  2016.       ext_tab = ((Elf32_External_gptab *)
  2017.              bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
  2018.       if (ext_tab == NULL)
  2019.         {
  2020.           bfd_set_error (bfd_error_no_memory);
  2021.           free (tab);
  2022.           return false;
  2023.         }
  2024.  
  2025.       for (i = 0; i < c; i++)
  2026.         bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
  2027.       free (tab);
  2028.  
  2029.       o->_raw_size = c * sizeof (Elf32_External_gptab);
  2030.       o->contents = (bfd_byte *) ext_tab;
  2031.  
  2032.       /* Skip this section later on (I don't think this currently
  2033.          matters, but someday it might).  */
  2034.       o->link_order_head = (struct bfd_link_order *) NULL;
  2035.     }
  2036.     }
  2037.  
  2038.   /* Get a value for the GP register.  */
  2039.   if (elf_gp (abfd) == 0)
  2040.     {
  2041.       struct bfd_link_hash_entry *h;
  2042.  
  2043.       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
  2044.       if (h != (struct bfd_link_hash_entry *) NULL
  2045.       && h->type == bfd_link_hash_defined)
  2046.     elf_gp (abfd) = (h->u.def.value
  2047.              + h->u.def.section->output_section->vma
  2048.              + h->u.def.section->output_offset);
  2049.       else if (info->relocateable)
  2050.     {
  2051.       bfd_vma lo;
  2052.  
  2053.       /* Make up a value.  */
  2054.       lo = (bfd_vma) -1;
  2055.       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  2056.         {
  2057.           if (o->vma < lo
  2058.           && (strcmp (o->name, ".sbss") == 0
  2059.               || strcmp (o->name, ".sdata") == 0
  2060.               || strcmp (o->name, ".lit4") == 0
  2061.               || strcmp (o->name, ".lit8") == 0))
  2062.         lo = o->vma;
  2063.         }
  2064.       elf_gp (abfd) = lo + 0x8000;
  2065.     }
  2066.       else
  2067.     {
  2068.       /* If the relocate_section function needs to do a reloc
  2069.          involving the GP value, it should make a reloc_dangerous
  2070.          callback to warn that GP is not defined.  */
  2071.     }
  2072.     }
  2073.  
  2074.   /* Invoke the regular ELF backend linker to do all the work.  */
  2075.   if (! bfd_elf32_bfd_final_link (abfd, info))
  2076.     return false;
  2077.  
  2078.   /* Now write out the computed sections.  */
  2079.  
  2080.   if (reginfo_sec != (asection *) NULL)
  2081.     {
  2082.       Elf32_External_RegInfo ext;
  2083.  
  2084.       bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
  2085.       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
  2086.                       (file_ptr) 0, sizeof ext))
  2087.     return false;
  2088.     }
  2089.  
  2090.   if (mdebug_sec != (asection *) NULL)
  2091.     {
  2092.       BFD_ASSERT (abfd->output_has_begun);
  2093.       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
  2094.                            swap, info,
  2095.                            mdebug_sec->filepos))
  2096.     return false;
  2097.  
  2098.       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
  2099.     }
  2100.  
  2101.   if (gptab_data_sec != (asection *) NULL)
  2102.     {
  2103.       if (! bfd_set_section_contents (abfd, gptab_data_sec,
  2104.                       gptab_data_sec->contents,
  2105.                       (file_ptr) 0,
  2106.                       gptab_data_sec->_raw_size))
  2107.     return false;
  2108.     }
  2109.  
  2110.   if (gptab_bss_sec != (asection *) NULL)
  2111.     {
  2112.       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
  2113.                       gptab_bss_sec->contents,
  2114.                       (file_ptr) 0,
  2115.                       gptab_bss_sec->_raw_size))
  2116.     return false;
  2117.     }
  2118.  
  2119.   return true;
  2120. }
  2121.  
  2122. /* Handle a MIPS ELF HI16 reloc.  */
  2123.  
  2124. static void
  2125. mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
  2126.      bfd *input_bfd;
  2127.      Elf_Internal_Rela *relhi;
  2128.      Elf_Internal_Rela *rello;
  2129.      bfd_byte *contents;
  2130.      bfd_vma addend;
  2131. {
  2132.   bfd_vma insn;
  2133.   bfd_vma addlo;
  2134.  
  2135.   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
  2136.  
  2137.   addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
  2138.   addlo &= 0xffff;
  2139.  
  2140.   addend += ((insn & 0xffff) << 16) + addlo;
  2141.  
  2142.   if ((addlo & 0x8000) != 0)
  2143.     addend -= 0x10000;
  2144.   if ((addend & 0x8000) != 0)
  2145.     addend += 0x10000;
  2146.  
  2147.   bfd_put_32 (input_bfd,
  2148.           (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
  2149.           contents + relhi->r_offset);
  2150. }
  2151.  
  2152. /* Relocate a MIPS ELF section.  */
  2153.  
  2154. static boolean
  2155. mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
  2156.                contents, relocs, local_syms, local_sections,
  2157.                output_names)
  2158.      bfd *output_bfd;
  2159.      struct bfd_link_info *info;
  2160.      bfd *input_bfd;
  2161.      asection *input_section;
  2162.      bfd_byte *contents;
  2163.      Elf_Internal_Rela *relocs;
  2164.      Elf_Internal_Sym *local_syms;
  2165.      asection **local_sections;
  2166.      char *output_names;
  2167. {
  2168.   Elf_Internal_Shdr *symtab_hdr;
  2169.   size_t locsymcount;
  2170.   size_t extsymoff;
  2171.   Elf_Internal_Rela *rel;
  2172.   Elf_Internal_Rela *relend;
  2173.  
  2174.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2175.  
  2176.   if (elf_bad_symtab (input_bfd))
  2177.     {
  2178.       locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
  2179.       extsymoff = 0;
  2180.     }
  2181.   else
  2182.     {
  2183.       locsymcount = symtab_hdr->sh_info;
  2184.       extsymoff = symtab_hdr->sh_info;
  2185.     }
  2186.  
  2187.   rel = relocs;
  2188.   relend = relocs + input_section->reloc_count;
  2189.   for (; rel < relend; rel++)
  2190.     {
  2191.       int r_type;
  2192.       const reloc_howto_type *howto;
  2193.       long r_symndx;
  2194.       bfd_vma addend;
  2195.       struct elf_link_hash_entry *h;
  2196.       asection *sec;
  2197.       Elf_Internal_Sym *sym;
  2198.       bfd_reloc_status_type r;
  2199.  
  2200.       r_type = ELF32_R_TYPE (rel->r_info);
  2201.       if (r_type < 0 || r_type >= (int) R_MIPS_max)
  2202.     {
  2203.       bfd_set_error (bfd_error_bad_value);
  2204.       return false;
  2205.     }
  2206.       howto = elf_mips_howto_table + r_type;
  2207.  
  2208.       r_symndx = ELF32_R_SYM (rel->r_info);
  2209.  
  2210.       /* Mix in the change in GP address for a GP relative reloc.  */
  2211.       if (r_type != R_MIPS_GPREL16
  2212.       && r_type != R_MIPS_LITERAL
  2213.       && r_type != R_MIPS_GPREL32)
  2214.     addend = 0;
  2215.       else
  2216.     {
  2217.       if (elf_gp (output_bfd) == 0)
  2218.         {
  2219.           if (! ((*info->callbacks->reloc_dangerous)
  2220.              (info,
  2221.               "GP relative relocation when GP not defined",
  2222.               input_bfd, input_section,
  2223.               rel->r_offset)))
  2224.         return false;
  2225.           /* Only give the error once per link.  */
  2226.           elf_gp (output_bfd) = 4;
  2227.         }
  2228.  
  2229.       if (r_symndx < extsymoff
  2230.           || (elf_bad_symtab (input_bfd)
  2231.           && local_sections[r_symndx] != NULL))
  2232.         {
  2233.           /* This is a relocation against a section.  The current
  2234.          addend in the instruction is the difference between
  2235.          INPUT_SECTION->vma and the GP value of INPUT_BFD.  We
  2236.          must change this to be the difference between the
  2237.          final definition (which will end up in RELOCATION)
  2238.          and the GP value of OUTPUT_BFD (which is in GP).  */
  2239.           addend = elf_gp (input_bfd) - elf_gp (output_bfd);
  2240.         }
  2241.       else if (! info->relocateable)
  2242.         {
  2243.           /* We are doing a final link.  The current addend in the
  2244.          instruction is simply the desired offset into the
  2245.          symbol (normally zero).  We want the instruction to
  2246.          hold the difference between the final definition of
  2247.          the symbol (which will end up in RELOCATION) and the
  2248.          GP value of OUTPUT_BFD (which is in GP).  */
  2249.           addend = - elf_gp (output_bfd);
  2250.         }
  2251.       else
  2252.         {
  2253.           /* We are generating relocateable output, and we aren't
  2254.          going to define this symbol, so we just leave the
  2255.          instruction alone.  */
  2256.           addend = 0;
  2257.         }
  2258.     }
  2259.  
  2260.       h = NULL;
  2261.       sym = NULL;
  2262.       sec = NULL;
  2263.       if (info->relocateable)
  2264.     {
  2265.       /* This is a relocateable link.  We don't have to change
  2266.          anything, unless the reloc is against a section symbol,
  2267.          in which case we have to adjust according to where the
  2268.          section symbol winds up in the output section.  */
  2269.       if (r_symndx >= locsymcount
  2270.           || (elf_bad_symtab (input_bfd)
  2271.           && local_sections[r_symndx] == NULL))
  2272.         r = bfd_reloc_ok;
  2273.       else
  2274.         {
  2275.           sym = local_syms + r_symndx;
  2276.           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  2277.         r = bfd_reloc_ok;
  2278.           else
  2279.         {
  2280.           sec = local_sections[r_symndx];
  2281.  
  2282.           /* It would be logical to add sym->st_value here,
  2283.              but Irix 5 sometimes generates a garbage symbol
  2284.              value.  */
  2285.           addend += sec->output_offset;
  2286.  
  2287.           /* If this is HI16 with an associated LO16, adjust
  2288.              the addend accordingly.  Otherwise, just
  2289.              relocate.  */
  2290.           if (r_type != R_MIPS_HI16
  2291.               || (rel + 1) >= relend
  2292.               || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
  2293.             r = _bfd_relocate_contents (howto, input_bfd,
  2294.                         addend,
  2295.                         contents + rel->r_offset);
  2296.           else
  2297.             {
  2298.               mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
  2299.                           contents, addend);
  2300.               r = bfd_reloc_ok;
  2301.             }
  2302.         }
  2303.         }
  2304.     }
  2305.       else
  2306.     {
  2307.       bfd_vma relocation;
  2308.  
  2309.       /* This is a final link.  */
  2310.       sym = NULL;
  2311.       if (r_symndx < extsymoff
  2312.           || (elf_bad_symtab (input_bfd)
  2313.           && local_sections[r_symndx] != NULL))
  2314.         {
  2315.           sym = local_syms + r_symndx;
  2316.           sec = local_sections[r_symndx];
  2317.           relocation = (sec->output_section->vma
  2318.                 + sec->output_offset);
  2319.  
  2320.           /* It would be logical to always add sym->st_value here,
  2321.          but Irix 5 sometimes generates a garbage symbol
  2322.          value.  */
  2323.           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  2324.         relocation += sym->st_value;
  2325.         }
  2326.       else
  2327.         {
  2328.           long indx;
  2329.  
  2330.           indx = r_symndx - extsymoff;
  2331.           h = elf_sym_hashes (input_bfd)[indx];
  2332.           if (h->root.type == bfd_link_hash_defined)
  2333.         {
  2334.           sec = h->root.u.def.section;
  2335.           relocation = (h->root.u.def.value
  2336.                 + sec->output_section->vma
  2337.                 + sec->output_offset);
  2338.         }
  2339.           else if (h->root.type == bfd_link_hash_weak)
  2340.         relocation = 0;
  2341.           else
  2342.         {
  2343.           if (! ((*info->callbacks->undefined_symbol)
  2344.              (info, h->root.root.string, input_bfd,
  2345.               input_section, rel->r_offset)))
  2346.             return false;
  2347.           relocation = 0;
  2348.         }
  2349.         }
  2350.  
  2351.       if (r_type != R_MIPS_HI16
  2352.           || (rel + 1) >= relend
  2353.           || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
  2354.         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2355.                       contents, rel->r_offset,
  2356.                       relocation, addend);
  2357.       else
  2358.         {
  2359.           mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
  2360.                       contents, relocation + addend);
  2361.           r = bfd_reloc_ok;
  2362.         }
  2363.     }
  2364.  
  2365.       if (r != bfd_reloc_ok)
  2366.     {
  2367.       switch (r)
  2368.         {
  2369.         default:
  2370.         case bfd_reloc_outofrange:
  2371.           abort ();
  2372.         case bfd_reloc_overflow:
  2373.           {
  2374.         const char *name;
  2375.  
  2376.         if (h != NULL)
  2377.           name = h->root.root.string;
  2378.         else
  2379.           {
  2380.             name = output_names + sym->st_name;
  2381.             if (name == NULL)
  2382.               return false;
  2383.             if (*name == '\0')
  2384.               name = bfd_section_name (input_bfd, sec);
  2385.           }
  2386.         if (! ((*info->callbacks->reloc_overflow)
  2387.                (info, name, howto->name, (bfd_vma) 0,
  2388.             input_bfd, input_section, rel->r_offset)))
  2389.           return false;
  2390.           }
  2391.           break;
  2392.         }
  2393.     }
  2394.     }
  2395.  
  2396.   return true;
  2397. }
  2398.  
  2399. /* ECOFF swapping routines.  These are used when dealing with the
  2400.    .mdebug section, which is in the ECOFF debugging format.  */
  2401. static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap =
  2402. {
  2403.   /* Symbol table magic number.  */
  2404.   magicSym,
  2405.   /* Alignment of debugging information.  E.g., 4.  */
  2406.   4,
  2407.   /* Sizes of external symbolic information.  */
  2408.   sizeof (struct hdr_ext),
  2409.   sizeof (struct dnr_ext),
  2410.   sizeof (struct pdr_ext),
  2411.   sizeof (struct sym_ext),
  2412.   sizeof (struct opt_ext),
  2413.   sizeof (struct fdr_ext),
  2414.   sizeof (struct rfd_ext),
  2415.   sizeof (struct ext_ext),
  2416.   /* Functions to swap in external symbolic data.  */
  2417.   ecoff_swap_hdr_in,
  2418.   ecoff_swap_dnr_in,
  2419.   ecoff_swap_pdr_in,
  2420.   ecoff_swap_sym_in,
  2421.   ecoff_swap_opt_in,
  2422.   ecoff_swap_fdr_in,
  2423.   ecoff_swap_rfd_in,
  2424.   ecoff_swap_ext_in,
  2425.   _bfd_ecoff_swap_tir_in,
  2426.   _bfd_ecoff_swap_rndx_in,
  2427.   /* Functions to swap out external symbolic data.  */
  2428.   ecoff_swap_hdr_out,
  2429.   ecoff_swap_dnr_out,
  2430.   ecoff_swap_pdr_out,
  2431.   ecoff_swap_sym_out,
  2432.   ecoff_swap_opt_out,
  2433.   ecoff_swap_fdr_out,
  2434.   ecoff_swap_rfd_out,
  2435.   ecoff_swap_ext_out,
  2436.   _bfd_ecoff_swap_tir_out,
  2437.   _bfd_ecoff_swap_rndx_out,
  2438.   /* Function to read in symbolic data.  */
  2439.   mips_elf_read_ecoff_info
  2440. };
  2441.  
  2442. #define TARGET_LITTLE_SYM        bfd_elf32_littlemips_vec
  2443. #define TARGET_LITTLE_NAME        "elf32-littlemips"
  2444. #define TARGET_BIG_SYM            bfd_elf32_bigmips_vec
  2445. #define TARGET_BIG_NAME            "elf32-bigmips"
  2446. #define ELF_ARCH            bfd_arch_mips
  2447. #define ELF_MACHINE_CODE        EM_MIPS
  2448. #define ELF_MAXPAGESIZE            0x10000
  2449. #define elf_backend_collect        true
  2450. #define elf_info_to_howto        0
  2451. #define elf_info_to_howto_rel        mips_info_to_howto_rel
  2452. #define elf_backend_sym_is_global    mips_elf_sym_is_global
  2453. #define elf_backend_object_p        mips_elf_object_p
  2454. #define elf_backend_section_from_shdr    mips_elf_section_from_shdr
  2455. #define elf_backend_fake_sections    mips_elf_fake_sections
  2456. #define elf_backend_section_from_bfd_section \
  2457.                     mips_elf_section_from_bfd_section
  2458. #define elf_backend_section_processing    mips_elf_section_processing
  2459. #define elf_backend_symbol_processing    mips_elf_symbol_processing
  2460. #define elf_backend_final_write_processing \
  2461.                     mips_elf_final_write_processing
  2462. #define elf_backend_ecoff_debug_swap    &mips_elf_ecoff_debug_swap
  2463.  
  2464. #define bfd_elf32_bfd_link_hash_table_create \
  2465.                     mips_elf_link_hash_table_create
  2466. #define bfd_elf32_bfd_final_link    mips_elf_final_link
  2467. #define elf_backend_relocate_section    mips_elf_relocate_section
  2468. #define elf_backend_add_symbol_hook    mips_elf_add_symbol_hook
  2469.  
  2470. #include "elf32-target.h"
  2471.